﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LMS.CommClass;

namespace LMS
{
    public partial class FrmReissueCard : Form
    {
        IssuedCard _issuedCard = new IssuedCard();
        IssuedCardLogic LIssuedCard = new IssuedCardLogic(App.Ds);
        public string IssuedCardNumber;
        CardReader _cardReader = new CardReader();
        CardReaderLogic LCardReader = new CardReaderLogic(App.Ds);
        Site _site = new Site();

        public FrmReissueCard()
        {
            InitializeComponent();
        }

        private void FrmReissueCard_Shown(object sender, EventArgs e)
        {
            FlowTypeLogic LFlowType = new FlowTypeLogic(App.Ds);

            _issuedCard = LIssuedCard.GetIssuedCardByNumber(IssuedCardNumber);
            txtVehicleNumber.Text = _issuedCard.VehicleNumber;
            txtQueueNumber.Text = _issuedCard.QueueNumber;
            txtDONumber.Text = _issuedCard.DONumber;
            txtDriver.Text = _issuedCard.Driver;
            txtMobileNumber.Text = _issuedCard.MobileNumber;
            txtIDNumber.Text = _issuedCard.IDNumber;
            txtRemark.Text = _issuedCard.Remark;
            txtFlow.Text = LFlowType.LoadByID(_issuedCard.FlowID).FlowName;
        }

        //连接读卡器，通过读卡器序列号，取得当前节点位置
        private bool ConnectCardReader()
        {
            if (!_cardReader.LoadSettingFromRegistry())
            {
                MessageBox.Show("读卡器未配置，请配置后使用！");
                return false;
            }

            if (!_cardReader.Connect())
            {
                MessageBox.Show("读卡器连接失败！");
                return false;
            }

            if (_cardReader.GetDeviceSerialNumber())
            {
                _site = LCardReader.GetSiteByCardReaderSN(_cardReader.SerialNumber.ToString());
            }
            else
            {
                MessageBox.Show("这个读卡机未注册，请注册后使用！");
                return false;
            }
            return true;
        }

        private void btnReissueCard_Click(object sender, EventArgs e)
        {
            int _cardID;
            ICS70 _S70Card = new ICS70();
            ICCardLogic LCard = new ICCardLogic(App.Ds);

            if (!ConnectCardReader())
            {
                return;
            }
            //读卡
            _cardReader.SelectCard(_S70Card);
            _cardID = LCard.GetCardIDBySN(_S70Card.GetSerialNumber().ToString());
            _cardReader.DisConnect();

            if (_cardID == 0)
            {
                MessageBox.Show("当前IC卡未注册，请使用注册的IC卡！");
                return;
            }
            if (LIssuedCard.ReissueCard(IssuedCardNumber, _cardID, App.AppUser.UserID, _site.SiteID))
            {
                if (ConnectCardReader())
                {
                    _cardReader.Beep(10, 1);
                }
                MessageBox.Show("卡补发成功！");
                DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                MessageBox.Show("补发卡出错："+LIssuedCard.Message);
                return;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
